package com.badcat.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.util.HashMap;
import java.util.Map;

/**
 * @author ：badcat
 * @date ：Created in 2022/6/25 17:11
 * @description ：
 */
@RestController
@RequestMapping("/formDataPiece")
public class FormDataPieceController {

    private final String path = "D:\\study\\project\\2022study\\gitee\\file_upload\\formDataPiece";

    @PostMapping("/upload")
    public Map<String, Object> upload(MultipartFile file, String fileKey, String fileName, Integer fileIndex) throws Exception{
        String contentType = file.getContentType();
        String name = file.getName();
        String originalFilename = file.getOriginalFilename();
        System.err.println(contentType);
        System.err.println(name);
        System.err.println(originalFilename);


        File fileDirectory = new File(path);
        if (!fileDirectory.exists()){
            fileDirectory.mkdirs();
        }

        if(fileIndex == 0){
            file.transferTo(new File(path + "\\" + fileKey + "_" + fileName));
        } else {
            file.transferTo(new File(path + "\\" + fileKey + "_" + fileName + ".tmp" + fileIndex));
        }

        Map<String, Object> map = new HashMap<>();
        map.put("fileIndex", fileIndex);
        map.put("fileSize", file.getSize());
        return map;
    }

    @PostMapping("/mergeFile")
    public String mergeFile(String fileKey) throws Exception{
        File fileDirectory = new File(path);
        File[] files = fileDirectory.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return name.startsWith(fileKey);
            }
        });

        File realFile = null;

        for (int i = 0; i < files.length; i++){
            File file = files[i];
            String name = file.getName();
            if(name.startsWith(fileKey) && !name.endsWith(".tmp" + i)){
                realFile = file;
                break;
            }
        }

        for (int j = 0; j < files.length; j++){
            for (int i = 0; i < files.length; i++){
                File file = files[i];
                String name = file.getName();
                if (name.startsWith(fileKey) && name.endsWith(".tmp" + j)){
                    FileInputStream fileInputStream = new FileInputStream(file);
                    FileOutputStream fileOutputStream = new FileOutputStream(realFile, true);
                    byte[] bytes = new byte[1024];
                    int b;
                    while ((b = fileInputStream.read(bytes)) != -1){
                        fileOutputStream.write(bytes, 0, b);
                    }

                    fileInputStream.close();
                    fileOutputStream.close();

                    file.delete();
                    break;
                }
            }
        }

        return "ok";
    }

    @PostMapping("/test")
    public String test(){
        return "test";
    }
}
